﻿using System;
using System.Globalization;
using System.IO;
using System.Xml.Serialization;

namespace Utilities
{
    public class ConvertUtility
    {
        #region Int & IntN Parse

        /// <summary>
        ///     Parse value from bool to integer, True = 1 and False = 0
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int IntParse(bool b)
        {
            return b ? 1 : 0;
        }

        /// <summary>
        ///     Parse value from bool to integer, True = 1 and False = 0. If input value is null, return null
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int IntParse(bool? b)
        {
            return (b.HasValue && b.Value) ? 1 : 0;
        }

        /// <summary>
        ///     Parse value from object to integer. If object value is not integer, return 0
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static int IntParse(object o)
        {
            if (o == null) return 0;
            int i;
            if (int.TryParse(o.ToString(), NumberStyles.Any, Utility.Info, out i))
            {
            }
            return i;
        }

        /// <summary>
        ///     Parse value from object to integer. If object value is not integer, return null
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static int? IntNParse(object o)
        {
            if (o == null) return null;
            return IntParse(o);
        }

        /// <summary>
        ///     Parse value from decimal to integer.
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static int IntParse(decimal d)
        {
            return (int) d;
        }

        /// <summary>
        ///     Parse value from decimal to integer. If decimal value == 0, return null
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static int? IntNParse(decimal d)
        {
            if (d == 0) return null;
            return IntParse(d);
        }

        /// <summary>
        ///     Parse value from decimal? to integer. If decimal? is zero or null, return 0
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static int IntParse(decimal? d)
        {
            if (d == null) return 0;
            return IntParse(d.Value);
        }

        /// <summary>
        ///     Parse value from decimal? to integer. If decimal? is zero or null, return null
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static int? IntNParse(decimal? d)
        {
            if (d == null) return null;
            return IntNParse(d.Value);
        }

        #endregion Int & IntN Parse

        #region Decimal & DecimalN Parse

        /// <summary>
        ///     Parse value from object to integer. If object value is not decimal, return 0
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static decimal DecimalParse(object o)
        {
            if (o == null) return 0;
            decimal de;
            if (decimal.TryParse(o.ToString(), NumberStyles.Any, Utility.Info, out de))
            {
            }
            return de;
        }

        /// <summary>
        ///     Parse value from object to integer. If object value is not decimal, return null
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static decimal? DecimalNParse(object o)
        {
            if (o == null) return null;
            return DecimalParse(o);
        }

        #endregion Decimal & DecimalN Parse

        #region String Parse

        /// <summary>
        /// Return string with thousands seperator
        /// </summary>
        /// <param name="de"></param>
        /// <returns></returns>
        public static string StringParse(decimal de)
        {
            return String.Format(Utility.Info, "{0:N}", de);
        }

        /// <summary>
        /// Return string with thousands seperator. If input value is null, return "0"
        /// </summary>
        /// <param name="de"></param>
        /// <returns></returns>
        public static string StringParse(decimal? de)
        {
            if (!de.HasValue) return "0";
            return StringParse(de.Value);
        }

        /// <summary>
        /// Return string with thousands seperator. If input value is null, return Empty
        /// </summary>
        /// <param name="de"></param>
        /// <returns></returns>
        public static string StringNParse(decimal? de)
        {
            if (!de.HasValue || de == 0M)
            {
                return String.Empty;
            }
            return StringParse(de.Value);
        }

        public static string StringParse(byte[] b)
        {
            if (b == null) return String.Empty;
            return BitConverter.ToString(b);
        }

        public static string StringParse(object o)
        {
            return o == null ? String.Empty : o.ToString();
        }

        #endregion String Parse

        #region DateTime Parse

        public static DateTime DateTimeParse(object o)
        {
            if (o == null) return DateTime.MinValue;
            DateTime dt;
            if (DateTime.TryParse(o.ToString(), Utility.Info, DateTimeStyles.None, out dt))
            {
            }
            return dt;
        }

        public static DateTime? DateTimeNParse(object o)
        {
            if (o == null) return null;
            DateTime dt;
            if (DateTime.TryParse(o.ToString(), Utility.Info, DateTimeStyles.None, out dt))
                return dt;
            return null;
        }

        #endregion DateTime Parse

        #region Bool Parse

        public static bool BoolParse(object o)
        {
            if (o == null) return false;
            bool b;
            if (bool.TryParse(o.ToString(), out b))
            {
            }
            return b;
        }

        public static bool BoolParse(int i)
        {
            return i == 1;
        }

        #endregion

        #region Xml

        public static string XmlStringParse(object obj)
        {
            var serializer = new XmlSerializer(obj.GetType());
            var sw = new StringWriter();

            serializer.Serialize(sw, obj);
            return sw.ToString();
        }

        #endregion
    }
}